﻿
using System;
using System.Collections.Generic;
using Common;
using System.Text;
using System.Web;
using System.Configuration;
using System.Web.Configuration;
using System.Text.RegularExpressions;
using System.Xml;

namespace Common
{
    /// <summary>
    /// 站点配置信息类,记录所有网站相关的参数
    /// </summary>
    public class SiteInfo
    {
        #region 获取站点根目录URI

        /// <summary>
        /// 获取站点根目录URI,结尾始终包含“/”符号,如"http://www.siteroot.com/"
        /// </summary>
        public static string RootURI
        {
            get
            {
                string AppPath = "";
                HttpContext HttpCurrent = HttpContext.Current;
                HttpRequest Req;
                if (HttpCurrent != null)
                {
                    Req = HttpCurrent.Request;

                    string UrlAuthority = Req.Url.GetLeftPart(UriPartial.Authority);
                    if (Req.ApplicationPath == null || Req.ApplicationPath == "/")
                        //直接安装在   Web   站点   
                        AppPath = UrlAuthority;
                    else
                        //安装在虚拟子目录下   
                        AppPath = UrlAuthority + Req.ApplicationPath;
                }
                string s = StringPlus.LastBacklash(AppPath);
                return s;
            }
        }

        #endregion

        #region 取得网站根目录的物理路径,结尾始终包含“\”符号

        /// <summary>
        /// 取得网站根目录的物理路径,结尾始终包含“\”符号
        /// </summary>
        /// <returns></returns>
        public static string RootPath
        {
            get
            {
                string AppPath = "";
                HttpContext HttpCurrent = HttpContext.Current;
                if (HttpCurrent != null)
                {
                    AppPath = HttpCurrent.Server.MapPath("~");
                }
                else
                {
                    AppPath = AppDomain.CurrentDomain.BaseDirectory;
                    if (Regex.Match(AppPath, @"\\$", RegexOptions.Compiled).Success)
                        AppPath = AppPath.Substring(0, AppPath.Length - 1);
                }
                if (AppPath.Substring(AppPath.Length - 1, 1) != "\\")
                    AppPath += "\\";

                return AppPath;
            }
        }

        #endregion

        #region 获取前台网站的配置文件的物理路径

        /// <summary>
        /// 获取前台网站的配置文件的物理路径
        /// </summary>
        /// <returns></returns>
        public static string GetClientSiteConfigPath()
        {
            string path = ConfigurationSettings.AppSettings["SiteConfigFile"];
            return RootPath + path;
        }

        /// <summary>
        /// 获取SiteInfo.config文件的是否使用企业用户的节点
        /// </summary>
        public static string HaveEntUser
        {
            get { return XmlReader.GetConfigValue("haveEntUser", GetClientSiteConfigPath()); }
        }
        /// <summary>
        /// 获取SiteInfo.config文件的是否发布的节点
        /// </summary>
        public static string IsRelease
        {
            get { return XmlReader.GetConfigValue("release", GetClientSiteConfigPath()); }
        }

        /// <summary>
        /// 获取邮箱的SMTPServer
        /// </summary>
        /// <returns></returns>
        public static string GetSMTPServerPath()
        {
            string SMTP = ConfigurationSettings.AppSettings["SMTPServer"];
            return SMTP;
        }

        public static string GetFromMail()
        {
            string fromMail = ConfigurationSettings.AppSettings["FromMail"];
            return fromMail;
        }

        public static string GetPassWord()
        {
            string passWord = ConfigurationSettings.AppSettings["PassWord"];
            return passWord;
        }

        #endregion

        #region 获取前台网站的根路径

        /// <summary>
        /// 获取前台网站的根物理路径
        /// </summary>
        /// <returns></returns>
        public static String GetClientRootPath()
        {
            return XmlReader.GetConfigValue("rootPath", GetClientSiteConfigPath());
        }

        #endregion

        #region 全文索引配置
        /// <summary>
        /// 获取全文索引的文件路径
        /// </summary>
        /// <param name="type">类型</param>
        /// <returns></returns>
        public static String GetFullTextPath(string type)
        {
            type = type + "FullTextPath";
            return XmlReader.GetConfigValue(type, GetClientSiteConfigPath());
        }
        /// <summary>
        /// 获取全文索引的查询字段
        /// </summary>
        /// <param name="type">类型</param>
        /// <returns></returns>
        public static String GetFullTextField(string type)
        {
            type = type + "FullTextField";
            return XmlReader.GetConfigValue(type, GetClientSiteConfigPath());
        }
        /// <summary>
        /// 全文索引开关
        /// </summary>
        public static bool createFullTextFlag
        {
            get
            {
                string flag = XmlReader.GetConfigValue("createFullText", GetClientSiteConfigPath());
                if (string.IsNullOrEmpty(flag)) return false;
                if (flag.ToLower().Equals("true"))
                    return true;
                else
                    return false;
            }
        }
        #endregion

        #region 获取前台网站的HTML生成路径

        /// <summary>
        /// 获取前台网站的HTML生成路径
        /// </summary>
        /// <returns></returns>
        public static string GetClientHTMLSavePath()
        {
            return XmlReader.GetConfigValue("htmlSavePath", GetClientSiteConfigPath());
        }

        #endregion

        #region 获取前台网站的模板所在路径

        /// <summary>
        /// 获取前台网站的模板所在路径
        /// </summary>
        /// <returns></returns>
        public static string GetClientTemplatePath()
        {
            return XmlReader.GetConfigValue("templatePath", GetClientSiteConfigPath());
        }

        #endregion

        #region 是否本地调试状态

        /// <summary>
        /// 是否本地调试状态
        /// </summary>
        public static bool IsDebug
        {
            get
            {
                System.Xml.XmlNode node = XmlReader.GetSingleNode("system.web/compilation", RootPath + "web.config");
                string val = node.Attributes["debug"].Value;
                return val == "true";

                /* 下面这种方式不知和原因总是为false,暂停使用
                Configuration configuration = WebConfigurationManager.OpenWebConfiguration(null);
                SystemWebSectionGroup ws = (SystemWebSectionGroup)configuration.GetSectionGroup("system.web");

                return ws.Compilation.Debug;
                 */
            }
        }

        #endregion

        #region 获取老后台网站域名

        /// <summary>
        /// 获取老后台网站域名
        /// </summary>
        public static string OldSiteDomain
        {
            get { return XmlReader.GetConfigValue("siteDomain", GetClientSiteConfigPath()); }
        }

        #endregion

        #region 获取网站是否使用BBS

        /// <summary>
        /// 获取网站是否使用BBS
        /// </summary>
        public static bool UseBBS
        {
            get
            {
                string str = XmlReader.GetConfigValue("UseBBS", GetClientSiteConfigPath());
                if (string.IsNullOrEmpty(str)) return false;
                //else return bool.Parse(str);
                else 
                {
                    if (str == "1")
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
        }

        #endregion

        #region 分页时的每页显示数据条数

        /// <summary>
        /// 分页时的每页显示数据条数(admin后台使用)
        /// </summary>
        public static int PageDataSize
        {
            get
            {
                return 20;
            }
        }

        /// <summary>
        /// 分页时的每页显示数据条数(网站前台使用)
        /// </summary>
        public static int ClientPageSize
        {
            get { return 6; }
        }

        #endregion

        #region 获取文件上传路径

        /// <summary>
        /// 获取文件上传路径,只返回站点根目录的相对路径
        /// </summary>
        /// <param name="isURL">ture表示url(以/结束)，false表示物理相对路径(以\结束)</param>
        /// <returns></returns>
        public static string GetUploadFilePath(bool isURL)
        {
            string configValue = XmlReader.GetConfigValue("uploadPath", GetClientSiteConfigPath());
            if (isURL)
            {
                if (!string.IsNullOrEmpty(configValue))
                    return StringPlus.LastBacklash(configValue);  //使用指定的路径
                else
                    return "upload/";       //使用默认路径

            }
            else
            {
                if (!string.IsNullOrEmpty(configValue))
                    return configValue + "\\";  //使用指定的路径
                else
                    return "upload\\";       //使用默认路径
            }
        }

        /// <summary>
        /// 获取用户文件上传路径的根,只返回站点根目录的相对路径，即文件管理里面所上传文件的目标根目录
        /// </summary>
        /// <param name="isURL">ture表示url(以/结束)，false表示物理相对路径(以\结束)</param>
        /// <returns></returns>
        public static string GetUploadFileRootForUser(bool isURL)
        {
            if (isURL)
            {
                return "upload/user/";       //使用默认路径
            }
            else
            {
                return "upload\\user\\";       //使用默认路径
            }
        }

        /// <summary>
        /// 获取最大上传文件字节数（默认4M）
        /// </summary>
        public static int UploadMaxLength
        {
            get { return 1024 * 1024 * 4; }
        }

        #endregion

        #region DiscuzNT论坛URL,整合key，密钥

        /// <summary>
        /// DiscuzNT论坛URL
        /// </summary>
        public static string BBS_URL
        {
            get { return XmlReader.GetConfigValue("bbsURL"); }
        }
        /// <summary>
        /// DiscuzNT论坛整合key
        /// </summary>
        public static string DNT_APIKey
        {
            get
            {
                return XmlReader.GetConfigValue("bbsAPIkey");
            }
        }

        /// <summary>
        /// DiscuzNT论坛整合密钥
        /// </summary>
        public static string DNT_APISecret
        {
            get { return XmlReader.GetConfigValue("bbsAPIsecret"); }
        }

        #endregion

        #region 获取网站的siteinfo.config文件中缩略图的尺寸大小
        public static string GetImgSize()
        {
            return XmlReader.GetConfigValue("imgsize", GetClientSiteConfigPath());
        }
        #endregion

        #region 获取网站地 siteinfo.config 文件中水印内容
        public static string GetAddText()
        {
            return XmlReader.GetConfigValue("drawString", GetClientSiteConfigPath());
        }
        #endregion

        #region 获取前台模板编辑前备份位置
        /// <summary>
        /// 获取前台模板编辑前备份位置
        /// </summary>
        public static string GetBfClientTemplatePath
        {
            get { return XmlReader.GetConfigValue("bftemplatePath", GetClientSiteConfigPath()); }
        }
        #endregion

        #region 获取前台模板默认状态文件夹路径
        public static string GetDfClientTemplatePath
        {
            get { return XmlReader.GetConfigValue("dftemplatePath", GetClientSiteConfigPath()); }
        }
        #endregion

        #region 获取FlightAPILog日志站点路径
        /// <summary>
        /// 获取FlightAPILog日志站点路径
        /// </summary>
        public static string GetFlightAPILogPath
        {
            get { return XmlReader.GetConfigValue("FlightAPILogPath", GetClientSiteConfigPath()); }
        }
        #endregion

        #region 获取公共交行专线接口地址
        /// <summary>
        /// 获取公共交行专线接口地址
        /// </summary>
        public static string GetBocomCardPayUrl
        {
            get { return XmlReader.GetConfigValue("BocomCardPayUrl", GetClientSiteConfigPath()); }
        }
        #endregion

        #region 获取EtermAPI接口地址
        /// <summary>
        /// 获取EtermAPI接口地址
        /// </summary>
        public static string GetEtermAPIUrl
        {
            get { return XmlReader.GetConfigValue("EtermAPIUrl", GetClientSiteConfigPath()); }
        }
        #endregion

        #region 自出票Excel地址

        /// <summary>
        /// 自出票导入文件存放路径
        /// </summary>
        public static string SelfTicketingImportPath
        {
            get
            {
                return XmlReader.GetSingleNode("Finance/SelfTicketing/ImportPath", GetClientSiteConfigPath()).InnerText;
            }
        }

        /// <summary>
        /// 自出票导出文件存放路径
        /// </summary>
        public static string SelfTicketingExportPath
        {
            get
            {
                return XmlReader.GetSingleNode("Finance/SelfTicketing/ExportPath", GetClientSiteConfigPath()).InnerText;

            }
        }

        /// <summary>
        /// 自出票导入文件本地存放路径
        /// </summary>
        public static string LocalImportFilePath
        {
            get
            {
                return XmlReader.GetSingleNode("Finance/SelfTicketing/LocalImportFilePath", GetClientSiteConfigPath()).InnerText;
            }
        }

        //下载文件路径
        public static string GetDownloadFileUrl
        {
            get
            {
                return XmlReader.GetSingleNode("Finance/SelfTicketing/DownloadFileUrl", GetClientSiteConfigPath()).InnerText; 
            }
        } 

        #endregion

        #region 获取公共支付查询接口地址
        /// <summary>
        /// 获取公共支付查询接口地址
        /// </summary>
        public static string GetBasePaySearchUrl
        {
            get { return XmlReader.GetConfigValue("BasePaySearchUrl", GetClientSiteConfigPath()); }
        }
        #endregion

        #region 获取400电话请求地址
        public static string OrderPhoneRequestUrl
        {
            get
            {
                return XmlReader.GetConfigValue( "OrderPhoneRequestUrl", GetClientSiteConfigPath() );
            }
        }
        #endregion

        /// <summary>
        /// 淘宝店采购代理B2CUSERID
        /// </summary>
        public static string TaobaoMerchantId
        {
            get
            {
                return XmlReader.GetConfigValue( "TaobaoMerchantId", GetClientSiteConfigPath() );
            }
        }

        /// <summary>
        /// 支付宝服务费率 默认是0.001
        /// </summary>
        public static decimal AlipayServiceRate
        {
            get
            {
                string strRate = XmlReader.GetConfigValue( "AlipayServiceRate", GetClientSiteConfigPath() );
                decimal dRate = 0.001m;
                decimal.TryParse( strRate, out dRate );

                return dRate;
            }
        }

        /// <summary>
        /// 同程自动付款账户 默认是tcjp_01@17u.com
        /// </summary>
        public static string TCProxyPaymentAccount
        {
            get
            {
                string account = XmlReader.GetConfigValue( "TCProxyPaymentAccount", GetClientSiteConfigPath() );
                return account ?? "tcjp_01@17u.com";
            }
        }

        /// <summary>
        /// 同程平台变更单退款通知地址 默认是：http://jp.17ujp.com/admin/distributor/ChangeOrderRefundNotify.aspx
        /// </summary>
        public static string TCChangeOrderRefundNotifyUrl
        {
            get
            {
                XmlNode node = XmlReader.GetSingleNode( "AlipayNotifyUrl/TCChangeOrderRefundNotifyUrl", GetClientSiteConfigPath() );
                if ( node != null )
                    return node.InnerText;
                else
                    return "";
            }
        }
        /// <summary>
        /// 同程平台采购订单退款通知地址 默认是：http://jp.17ujp.com/admin/distributor/TCAlipay_Notify.aspx
        /// </summary>
        public static string TCOrderRefundNotifyUrl
        {
            get
            {
                XmlNode node = XmlReader.GetSingleNode( "AlipayNotifyUrl/TCOrderRefundNotifyUrl", GetClientSiteConfigPath() );
                if ( node != null )
                    return node.InnerText;
                else
                    return "";
            }
        }
        /// <summary>
        /// 同行平台变更单退款通知地址 默认是：http://jp.17ujp.com/admin/distributor/ChangeOrderRefundNotify.aspx
        /// </summary>
        public static string PlatformChangeOrderRefundNotifyUrl
        {
            get
            {
                XmlNode node = XmlReader.GetSingleNode( "AlipayNotifyUrl/PlatformChangeOrderRefundNotifyUrl", GetClientSiteConfigPath() );
                if ( node != null )
                    return node.InnerText;
                else
                    return "";
            }
        }
        /// <summary>
        /// 同行平台订单退款通知地址 默认是：http://jp.17ujp.com/admin/distributor/Alipay_Notify.aspx
        /// </summary>
        public static string PlatformOrderRefundNotifyUrl
        {
            get
            {
                XmlNode node = XmlReader.GetSingleNode( "AlipayNotifyUrl/PlatformOrderRefundNotifyUrl", GetClientSiteConfigPath() );
                if ( node != null )
                    return node.InnerText;
                else
                    return "";
            }
        }

        #region 获取优惠券的Gudi
        public static string CouponGuid
        {
            get
            {
                return XmlReader.GetConfigValue("CouponGuid", GetClientSiteConfigPath());
            }
        }
        #endregion

        #region 获取优惠券的请求URL
        public static string CouponRequestUrl
        {
            get
            {
                return XmlReader.GetConfigValue("CouponRequestUrl", GetClientSiteConfigPath());
            }
        }
        #endregion

        #region 服务接口URL
        public static string GetServiceBalckScreenURL
        {
            get { return XmlReader.GetConfigValue("ServiceBalckScreenURL"); }
        } 
        #endregion

        #region BPT接口地址
        public static string GetBPTURL
        {
            get { return XmlReader.GetConfigValue("BPTURL"); }
        }
        #endregion

        #region 是否抵送优惠券接口参数

        #region 获取是否抵送优惠券的请求URL
        public static string TcAllianceCheckUrl
        {
            get
            {
                return XmlReader.GetConfigValue("CashCheckByRefIdUrl");
            }
        }
        #endregion

        #region 获取是否抵送优惠券的请求ID
        public static string TcAllianceCheckID
        {
            get
            {
                return XmlReader.GetConfigValue("CashCheckAccountID");
            }
        }
        #endregion

        #region 获取是否抵送优惠券的请求Key
        public static string TcAllianceCheckKey
        {
            get
            {
                return XmlReader.GetConfigValue("CashCheckAccountKey");
            }
        }
        #endregion

        #endregion

        #region 短信群发接口地址
        /// <summary>
        /// 短信群发接口地址
        /// </summary>
        public static string SMSMassURL
        {
            get { return XmlReader.GetConfigValue("SMSMassURL"); }
        }
        #endregion

        #region 短信单条发送接口地址
        /// <summary>
        /// 短信单条发送接口地址
        /// </summary>
        public static string SMSSendURL
        {
            get { return XmlReader.GetConfigValue("SMSSendURL"); }
        }
        #endregion

        #region 淘宝代扣公共地址
        /// <summary>
        /// 淘宝代扣公共地址
        /// </summary>
        public static string PayWithholdCAEUrl
        {
            get { return XmlReader.GetConfigValue("PayWithholdCAEUrl"); }
        }
        #endregion

        #region Eterm封装接口地址
        /// <summary>
        /// 获取对Eterm封装接口的地址
        /// </summary>
        public static string FlightEtermAPIUrl
        {
            get
            {
                XmlNode node = XmlReader.GetSingleNode( "FlightEtermAPI", GetClientSiteConfigPath() );
                if ( node == null )
                {
                    return "http://resource.17usoft.com/flighteterm/api.ashx";
                }
                else
                {
                    return node.Attributes["url"].InnerText;
                }
            }
        }
        #endregion

        #region 服务接口CN URL
        public static string GetCNServiceBalckScreenURL
        {
            get { return XmlReader.GetConfigValue("CNServiceBalckScreenURL"); }
        }
        #endregion
    }
}